#include "logindialog.h"
#include "loginview.h"
#include "registerview.h"
#include <QStackedLayout>

LoginDialog::LoginDialog(bool login, QWidget* parent)
    : QDialog(parent, Qt::MSWindowsFixedSizeDialogHint | Qt::WindowCloseButtonHint)
{
    setWindowTitle(login ? tr("Login") : tr("Register"));
    resize(340, 420);

    QStackedLayout* stacked_layout = new QStackedLayout(this);
    stacked_layout->setContentsMargins(0, 0, 0, 0);
    LoginView* view1 = new LoginView(this);
    stacked_layout->addWidget(view1);
    RegisterView* view2 = new RegisterView(this);
    stacked_layout->addWidget(view2);
    stacked_layout->setCurrentIndex(login ? 0 : 1);

    connect(view1, &LoginView::registerRequested, this, [=, this] {
        stacked_layout->setCurrentWidget(view2);
        setWindowTitle(tr("Register"));
    });
    connect(view1, &LoginView::cancelRequested, this, &QDialog::reject);
    connect(view1, &LoginView::finished, this, &LoginDialog::accept);
    connect(view2, &RegisterView::loginRequested, this, [=, this](const QString& name) {
        view1->setName(name);
        stacked_layout->setCurrentWidget(view1);
        setWindowTitle(tr("Login"));
    });
    connect(view2, &RegisterView::cancelRequested, this, &QDialog::reject);
}
